package com.example.WebTask1.Dao;

import com.example.WebTask1.Enity.Student;
import com.example.WebTask1.Until.DbUntil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class StudentDaoImp implements StudentDao{
    @Override
    public List<Student> queryStudents() {
        List<Student> list=new ArrayList<>();
        try {
            Connection connection = DbUntil.getConnection();
            String sql="select * from student ";
            PreparedStatement sql_statement = connection.prepareStatement(sql);
            ResultSet resultSet = sql_statement.executeQuery();

            while (resultSet.next()){
                list.add(new Student(resultSet.getInt(1),resultSet.getString(2),
                        resultSet.getString(3),
                        resultSet.getString(4),
                        resultSet.getString(5),
                        resultSet.getInt(6),
                        resultSet.getString(7)));
            }
            for (Student s:list
            ) {
                System.out.println(s.getName());
            }



        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return list;
    }

    @Override
    public Boolean deleteStudent(Student s) {
        Boolean b=false;
        try {
            Connection connection = DbUntil.getConnection();
            String sql="delete from student where id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setInt(1,s.getId());
            int i = preparedStatement.executeUpdate();
            if (i==1){
                b=true;
            }else {
                b=false;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return b;
    }

    @Override
    public Boolean insertStudent(Student s) {
        Boolean b=false;
        try {
            Connection connection = DbUntil.getConnection();
            String sql="INSERT into student VALUES(null,?,?,?,?,?,?);";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,s.getName());
            preparedStatement.setString(2,s.getSex());
            preparedStatement.setString(3,s.getBirthday());
            preparedStatement.setString(4,s.getBirthday());
            preparedStatement.setInt(5,s.getPhone());
            preparedStatement.setString(6,s.getComment());
            int i = preparedStatement.executeUpdate();
            if (i==1){
                b=true;
            }else {
                b=false;
            }

        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return b;
    }
    @Override
    public Boolean updateStudent(Student s) {
        Boolean b=false;
        try {
            Connection connection = DbUntil.getConnection();
            String sql="UPDATE student SET name=?, sex=?,birthday=?, emil=? ,phone=?, comment=? WHERE id=?";
            PreparedStatement preparedStatement = connection.prepareStatement(sql);
            preparedStatement.setString(1,s.getName());
            preparedStatement.setString(2,s.getSex());
            preparedStatement.setString(3,s.getBirthday());
            preparedStatement.setString(4,s.getEmil());
            preparedStatement.setString(5, String.valueOf(s.getPhone()));
            preparedStatement.setString(6,s.getComment());
            preparedStatement.setString(7, String.valueOf(s.getId()));
            int i = preparedStatement.executeUpdate();
            if (i==1){
                b=true;
            }else {
                b=false;
            }


        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }

        return b;
    }




}
